//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "KernelFunction.h"
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Math::CKernelFunction::CKernelFunction (boost::shared_ptr<QuantLib::KernelFunction>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phKernelFunction = NULL;
#endif
	_ppKernelFunction = &childNative;
    
}
Cephei::QL::Math::CKernelFunction::CKernelFunction (QuantLib::KernelFunction& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phKernelFunction = NULL;
#endif
	_ppKernelFunction = new boost::shared_ptr<QuantLib::KernelFunction> (&childNative);
    
    _KernelFunctionOwner = owner;
    
}

Cephei::QL::Math::CKernelFunction::CKernelFunction (CKernelFunction^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phKernelFunction = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppKernelFunction = new boost::shared_ptr<QuantLib::KernelFunction> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CKernelFunction::CKernelFunction (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phKernelFunction = NULL;
#endif
	if (!t->IsSubclassOf(CKernelFunction::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CKernelFunction::CKernelFunction (QuantLib::Handle<QuantLib::KernelFunction>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phKernelFunction = &childNative;
	_ppKernelFunction = &static_cast<boost::shared_ptr<QuantLib::KernelFunction>>(childNative.currentLink());
    
    _KernelFunctionOwner = owner;
}
Cephei::QL::Math::CKernelFunction::CKernelFunction (QuantLib::Handle<QuantLib::KernelFunction> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phKernelFunction = &childNative;
	_ppKernelFunction = &static_cast<boost::shared_ptr<QuantLib::KernelFunction>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CKernelFunction::CKernelFunction (QuantLib::KernelFunction childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phKernelFunction = NULL;
#endif
	_ppKernelFunction = new boost::shared_ptr<QuantLib::KernelFunction> (new QuantLib::KernelFunction (childNative));
    
}
#endif

Cephei::QL::Math::CKernelFunction::~CKernelFunction ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppKernelFunction != NULL)
    {
	    delete _ppKernelFunction;
        _ppKernelFunction = NULL;
    }
}
Cephei::QL::Math::CKernelFunction::!CKernelFunction ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppKernelFunction != NULL)
    {
	    delete _ppKernelFunction;
    }
}
QuantLib::KernelFunction& Cephei::QL::Math::CKernelFunction::GetReference ()
{
    if (_ppKernelFunction == NULL) throw gcnew NativeNullException ();
	return **_ppKernelFunction;
}
boost::shared_ptr<QuantLib::KernelFunction>& Cephei::QL::Math::CKernelFunction::GetShared ()
{
    if (_ppKernelFunction == NULL) throw gcnew NativeNullException ();
	return *_ppKernelFunction;
}
QuantLib::KernelFunction* Cephei::QL::Math::CKernelFunction::GetPointer ()
{
    if (_ppKernelFunction == NULL) throw gcnew NativeNullException ();
	return &**_ppKernelFunction;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::KernelFunction>& Cephei::QL::Math::CKernelFunction::GetHandle ()
{
	if (_phKernelFunction == NULL)
	{
		_phKernelFunction = new Handle<QuantLib::KernelFunction> (*_ppKernelFunction);
	}
	return *_phKernelFunction;
}
#endif
bool Cephei::QL::Math::CKernelFunction::HasNative () 
{
	return (_ppKernelFunction != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

